
/** 
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package com.tompai.common;

import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

/**
* @desc: datagather-common
* @name: DataBaseUtils.java
* @author: tompai
* @createTime: 2018年5月14日 上午11:29:36
* @history:
* @version: v1.0
*/

public class DataBaseUtils {

	/**
	 * filterEmojiChar: 替换四个字节的字符 '\xF0\x9F\x98\x84\xF0\x9F）的解决方案. <br/>
	 * 过滤掉iphone表情字符，解决入库失败问题 仅仅过滤掉表情字符，其他字符正常入库
	 */
	public String filterEmojiChar(String content) {

		if (StringUtils.isEmpty(content))
			return null;
		byte[] contentbyte = content.getBytes();
		for (int i = 0; i < contentbyte.length; i++) {
			if ((contentbyte[i] & 0xF8) == 0xF0) {
				for (int j = 0; j < 4; j++) {
					contentbyte[i + j] = 0x30;
				}
				i += 3;
			}
		}
		content = new String(contentbyte);
		return content.replaceAll("0000", "@_@");
	}

	public String addHtmlFormat(String content, String words) {
		String html = "";

		Random rand = new Random(System.currentTimeMillis());

		int color = rand.nextInt(255);
		color = (Math.abs(color)) % 0xFF;

		String markColor = "<span style=\"text-decoration: underline; background-color: rgb(255, 255, 0); color: rgb(255, 0, 0);\"><strong>";

		String replacement = markColor + words + "</strong></span>";

		html = commonReplace(content, words, replacement);

		html = "<p>" + html + "<br/></p>";

		return html;
	}

	public String commonReplace(String input, String regex, String replacement) {
		Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
		Matcher m = p.matcher(input);
		String result = m.replaceAll(replacement);
		return result;
	}

	/*
	 * 匹配字符串，不区分字母大小写;
	 */
	public boolean containsIgnoreCase(String input, String regex) {
		Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
		Matcher m = p.matcher(input);
		// String result = m.replaceAll(replacement);
		return m.matches();
	}

	/*
	 * 匹配字符串，不区分字母大小写;
	 */
	public String filterForwardTexts(String text, String regex) {
		
		if (!StringUtils.isEmpty(text) & StringUtils.contains(text, regex)) {
			int index = text.indexOf(regex);
			text = text.substring(0, index);
		}
		//转义HTML
		//StringEscapeUtils.escapeHtml4(text);
		return text;
	}
}
